package com.ww.yaodao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ww.yaodao.convert.AnnouncementConvert;
import com.ww.yaodao.mapper.AnnouncementMapper;
import com.ww.yaodao.model.bo.AnnouncementDO;
import com.ww.yaodao.model.vo.announce.AnnouncementRespVO;
import com.ww.yaodao.model.vo.announce.AnnouncementVO;
import com.ww.yaodao.service.AnnouncementService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author wanziyan
 * @Date 2023-04-9:21
 */
@Service
@RequiredArgsConstructor
public class AnnouncementServiceImpl implements AnnouncementService {

    private final AnnouncementMapper announcementMapper;
    @Override
    public AnnouncementRespVO getAllAnnounce() {
        List<AnnouncementDO> announcementDOS = announcementMapper.selectList(null);
        AnnouncementRespVO build = AnnouncementRespVO.builder().announcementDOList(announcementDOS).build();
        return build;
    }

    @Override
    public AnnouncementRespVO deleteAnnonce(Integer id) {
        Date date = new Date();
        SimpleDateFormat df =
                new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        LambdaQueryWrapper<AnnouncementDO> wrapper =
                new LambdaQueryWrapper<>();
        wrapper.eq(AnnouncementDO::getId,id);
        AnnouncementDO announcementDO = announcementMapper.selectOne(wrapper);
        announcementDO.setDelFlag("1");
        announcementDO.setDeleteTime(df.format(date));
        int update = announcementMapper.update(announcementDO, wrapper);
        AnnouncementRespVO allAnnounce = getAllAnnounce();
        allAnnounce.setRow(update);
        return allAnnounce;
    }


    @Override
    public AnnouncementRespVO updateById(AnnouncementVO announcementVO) {
        AnnouncementDO announcementDO = new AnnouncementDO();
        LambdaQueryWrapper<AnnouncementDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnnouncementDO::getId,announcementVO.getId());
        BeanUtils.copyProperties(announcementVO,announcementDO);
        if (announcementVO.getDelFlag().equals("0")){
            announcementVO.setDeleteTime(null);
        }
        int update = announcementMapper.update(announcementDO, wrapper);
        AnnouncementRespVO allAnnounce = getAllAnnounce();
        allAnnounce.setRow(update);
        return allAnnounce;
    }

    @Override
    public AnnouncementRespVO addAnnouncement(AnnouncementVO announcementVO) {
        AnnouncementDO announcementDO = new AnnouncementDO();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String dateTime = simpleDateFormat.format(date);
        announcementVO.setPublishTime(dateTime);
        BeanUtils.copyProperties(announcementVO,announcementDO);
        int insert = announcementMapper.insert(announcementDO);
        AnnouncementRespVO allAnnounce = getAllAnnounce();
        allAnnounce.setRow(insert);
        return allAnnounce;
    }
}
